<template>
<!-- 暂时没用 -->
  <Popover placement="bottom" v-model:open="visible" title="" trigger="click">
    <template #content>
      <ASelect
        v-model:value="currentOrg"
        v-if="userStore.userInfo?.adminType === 2"
        style="width: 250px"
        :options="options"
        @change="handleOrgChange"
      />
      <TreeSelect
        v-else
        v-model:value="currentOrg"
        style="width: 250px"
        :treeData="treeData"
        @change="handleOrgChange"
      />
    </template>
    <Tooltip title="组织选择">
    <a-button type="link"
      ><Icon icon="material-symbols:account-tree-outline-rounded"
    /></a-button>
  </Tooltip>
  </Popover>
</template>
<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { useUserStore } from '@/store/modules/user';
import { Select as ASelect, TreeSelect, Popover, Tooltip } from 'ant-design-vue';
import { getOrgList } from '@/api/sys/system';
import Icon from '@/components/Icon/Icon.vue';
import { TreeItem } from '@/components/Tree';
const userStore = useUserStore();
const currentOrg = ref(userStore.getCurrentOrg);
const options = ref<Recordable[]>([]);
const visible = ref<boolean>(false);
const handleOrgChange = (v, a) => {
  console.log(v, a, 'org change');
  userStore.setCurrentOrg(v);
  location.reload();
};
const treeData = ref<TreeItem[]>([]);
async function getAdminTree() {
  treeData.value = (await getOrgList({
    hasDepart: false,
  })) as unknown as TreeItem[];
}
onMounted(() => {
  const { orgs, adminType } = userStore.getUserInfo;
  if (adminType === 1) {
    getAdminTree();
  } else {
    options.value = orgs;
  }
});
</script>
